//当前多少sceneview加入了
var sceneList = {};
//当前ui层层级
var curZorder = 30;
var blackNode = null;
var SceneManager = (function(){
    function SceneManagerClass(){
        this._scenes = {};
        this._actions = {};
        this._curr_scene = null;
    }

    SceneManagerClass.prototype.init = function(){
        
    };

    SceneManagerClass.prototype.addScene = function(key, scene){
        if(this._scenes[key]){
            return false;
        }
        scene.init(key);
        this._scenes[key] = scene;
        return true;
    };

    SceneManagerClass.prototype.addAction = function(action_name, from, to){
        if(!this._actions[action_name]){
            this._actions[action_name] = {};
        }
        var action = this._actions[action_name];
        if(typeof from == "string"){
            if(from != '__any__'){
                from = this._scenes[from];
            }
        }
        if(typeof to == "string"){
            to = this._scenes[to];
        }
        action["from"] = from;
        action["name"] = action_name;
        action["to"] = to;
    };

    SceneManagerClass.prototype.toggleAction = function(action_name){
        
        if(curZorder > 0){
           curZorder -= 1;
        }
        if(!this._actions[action_name]){
            return false;
        }

        if(this._actions[action_name].from !== '__any__'){
            if(!(this._curr_scene === this._actions[action_name].from)){
                return false;
            }
        }
        var from = this._actions[action_name].from;
        var to = this._actions[action_name].to;
        if(from){
            if(typeof from == "object"){
                from.inActive();
            }else if(typeof from == "string"){
                if(this._curr_scene){
                    this._curr_scene.inActive();
                }else{
                    
                }
            }
        }
        //from = this._curr_scene;
        this._curr_scene = to;
        if(to){
            if(typeof to == "object"){
                to.active(from);
            }else if(typeof to == "string"){
                to = this._scenes[to];
                if(to){
                    to.active(from);
                }
            }
        }
    };

    SceneManagerClass.prototype.addBlack = function(view_key,_target,_showBlack){
        if(_showBlack == undefined || _showBlack == null){
           _showBlack = false;
        }
        curZorder += 1;
        if(_target == undefined || _target == null){
           sceneList[view_key] = null;
        }else{
           sceneList[view_key] = _target;
           _target.zOrder = curZorder;
        }
        if(blackNode == null && _showBlack){
            blackNode = new Laya.Sprite();
            blackNode.loadImage("wxlocal/black.png");
            Laya.stage.addChild(blackNode);
            blackNode.scaleX = Config.GameWidth / 125.0;
            blackNode.scaleY = (Config.GameHeight + offH) / 121.0;
            var blackMat =
			[
				0, 0, 0, 0, 0, //R
				0, 0, 0, 0, 0, //G
				0, 0, 0, 0, 0, //B
				0, 0, 0, 0.3, 0, //A
			];
            //创建一个颜色滤镜对象,黑色
            var blackFilter = new Laya.ColorFilter(blackMat);
            blackNode.filters = [blackFilter];
            blackNode.zOrder = curZorder - 1;
        }
    };

    SceneManagerClass.prototype.removeBlack = function(view_key){
        sceneList[view_key] = null;
        if(blackNode != null){
            blackNode.destroy();
            blackNode = null;
        }
    };


    SceneManagerClass.prototype.getScene = function(scene_key){
        return this._scenes[scene_key];
    };

    SceneManagerClass.prototype.getCurrScene = function(){
        return this._curr_scene;
    };

    return new SceneManagerClass();
})();
